<script lang="ts" setup>
import { deleteCardsAPI,
getCardListAPI, getCardProportionAPI } from '@/apis/card';
import type { CardItemType, CardListParamsType, CardProportionType } from '@/types/card';
import { WarningFilled } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter()

// 月卡列表 --------------------
// 请求参数
const cardListParams = ref<CardListParamsType>({
  page: 1,
  pageSize: 5,
  carNumber: undefined,
  personName: undefined,
  cardStatus: undefined
})
// 月卡列表
const cardList = ref<CardItemType[]>([])
const cardTotal = ref<number>(0) // 月卡总数
// 获取月卡列表
const getCardList = async () => {
  const res = await getCardListAPI(cardListParams.value)
  cardList.value = res.data.rows
  cardTotal.value = res.data.total
}
getCardList()
// 卡片状态
const cardStatus = (row: CardItemType) =>{
  return row.cardStatus === 0 ? '可用' : '已过期'
}

// 查询 -------------------- 重新请求
const onSearch = () => {
  cardListParams.value.page = 1 // 重置页码
  getCardList()
}

// 获取月卡占有率
const cardProportion = ref<CardProportionType>({} as CardProportionType)
const getCardProportion = async () => {
  const res = await getCardProportionAPI()
  cardProportion.value = res.data
}
getCardProportion()

// 表格选择框 -- 选中数据
const selectionCardList = ref<CardItemType[]>([])
const onSelectionChange = (val: CardItemType[]) => {
  selectionCardList.value = val
}

// 删除月卡 & 批量删除 --------------------
const deleteCards = async (ids: number[]) => {
  if (ids.length === 0) {
    ElMessage.warning('请选择要删除的数据')
    return
  }

  await ElMessageBox.confirm('此操作将删除所选月卡, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })

  const res = await deleteCardsAPI(ids)
  if(res.code === 10000) {
    ElMessage.success('删除成功')
  } else {
    ElMessage.error(res.msg)
  }
  getCardList()
}

// 续费
const xvFei = (id: number) => {
  router.push({
    name: 'cardAdd',
    query: { type: 'woyaofengle' },
    params: {
      id
    }
  })
}

</script>

<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input clearable v-model="cardListParams.carNumber" placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input clearable v-model="cardListParams.personName" placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select @clear="getCardList" clearable v-model="cardListParams.cardStatus" placeholder="全部" style="width: 240px">
        <el-option label="可用" value="0" />
        <el-option label="已过期" value="1" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="onSearch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <div class="left">
        <el-button type="primary" @click="$router.push('/cardAdd')" v-permission="'parking:card:add_edit'">添加月卡</el-button>
        <el-button @click="deleteCards(selectionCardList.map(item => item.id))" v-permission="'parking:card:remove'">批量删除</el-button>
      </div>
      <div class="right">
        <el-icon style="font-size: 15px; color: #1890ff"><WarningFilled /></el-icon>
        <span style="margin-left: 5px;">本园区共计 {{cardProportion.spaceNumber}} 个车位，月卡用户 {{cardProportion.cardCount}} 人，车位占有率 {{cardProportion.proportion}}</span>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="cardList" @selection-change="onSelectionChange">
        <el-table-column width="50" type="selection" />
        <el-table-column type="index" label="序号" width="100" align="center" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="cardStatus" />
        <el-table-column label="操作" fixed="right" width="280">
          <template #default="{ row }">
            <el-button size="small" type="primary" link @click="xvFei(row.id)" v-permission="'parking:card:recharge'">续费</el-button>
            <el-button size="small" type="primary" link @click="$router.push(`/cardWatch/${row.id}`)" v-permission="'parking:card:query'">查看</el-button>
            <el-button size="small" type="primary" link @click="$router.push(`/cardAdd/${row.id}`)" v-permission="'parking:card:add_edit'">编辑</el-button>
            <el-button size="small" type="primary" link @click="deleteCards([row.id])" v-permission="'parking:card:remove'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination 
        layout="total, sizes, prev, pager, next"
        :total="cardTotal"
        :page-sizes="[2,5,10,20]"
        v-model:current-page="cardListParams.page" 
        v-model:page-size="cardListParams.pageSize"
        @change="getCardList"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  display: flex;
  justify-content: space-between;
  margin: 20px 0px;
  .right {
    height: 35px;
    line-height: 35px;
    padding: 0px 10px;
    background-color: #e6f7ff;
    border: 1px solid #abdfff;
    border-radius: 5px;
    font-size: 13px;
    display: flex;
    align-items: center;
  }
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>